// components/amount/index.js
import { updateItemNum } from '@mall/api/shopCart'
import { deleteItem } from '@mall/api/shopCart'

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    count: { // 商品数量
      type: Number,
      value: 1
    },
    //限购数量
    limitcount: {
      type: Number,

    },
    otherBgColor: {
      type: String,
      value: '#f6f6f6'

    },
    centerBgColor: {
      type: String,
      value: '#e5e4e4'
    },
    goodsObj: {
      type: Object
    },
    shopType: {
      type: String,
      value: 'goods'

    },
    //减到0时，是否需要删除商品
    zeroIfDeleteGood: {
      type: Boolean,
      value: false
    }


  },
  options: {
    multipleSlots: true,
    addGlobalClass: true
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    //当输入框失去焦点时触发
    inputChangeHandle: function (event) {
      const self = this;
      console.log("event", event)
      let val = event.detail.value;
      //有商品对象时
      if (this.data.goodsObj) {
        let count = this.data.count;
        let goodsObj = this.data.goodsObj;
        console.log(' this.data.goodsObj', this.data.goodsObj);
        console.log('goodsObj', goodsObj)
        let cartUpdateGoodsNum = {
          cartItemId: goodsObj.cartItemId,
          goodsId: goodsObj.goodsId,
          num: val,
          skuId: goodsObj.skuId,
          storeId: goodsObj.storeId
        }
        updateItemNum(cartUpdateGoodsNum).then(res => {
          if (res.code == 0) {
            let eventDetailNum = {
              val: val,
              goodsObj: self.data.goodsObj
            }
            // 数据改变时 向父组件传递新数据
            this.triggerEvent('eventNum', eventDetailNum)
          }


        })
      } else {
        let num = parseInt(val);
        if (num > 0) {
          //是否限购
          if (this.data.limitcount > 0) {

            num = num > this.data.limitcount ? this.data.limitcount : num
          }
          let eventDetailNum = {
            val: num
          };
          this.triggerEvent('eventNum', eventDetailNum)
        } else {
          this.setData({
            count: 1
          })
        }
      }
    },


    //减
    subtract: function () {
      let count = this.data.count;
      if (this.data.goodsObj) {
        let goodsObj = this.data.goodsObj;
        console.log(' this.data.goodsObj', this.data.goodsObj);
        console.log('goodsObj', goodsObj)
        let cartUpdateGoodsNum = {
          cartItemId: goodsObj.cartItemId,
          goodsId: goodsObj.goodsId,
          num: count - 1,
          skuId: goodsObj.skuId,
          storeId: goodsObj.storeId
        }
        console.log('cartUpdateGoodsNum', cartUpdateGoodsNum);
        console.log('count', count)
        if (count - 1 < 1) {
          const self = this;
          wx.showToast({
            title: '该商品不能减少了噢',
            icon: 'none'
          })
          if (this.data.zeroIfDeleteGood) {
            let delTemp = {
              cartItemId: cartUpdateGoodsNum.cartItemId,
              storeId: cartUpdateGoodsNum.storeId
            }
            deleteItem(delTemp).then(res => {
              if (res.code == 0) {
                self.triggerEvent('deleteGoodsEvent')
              }
            })
          }

          //向父组件发出删除事件

          return;
        }
        updateItemNum(cartUpdateGoodsNum).then(res => {
          if (res.code == 0) {
            count > 1 ? count-- : 1
            this.setData({
              count: count
            })
            let eventDetailNum = {
              val: count,
              type: 'subevent',
              goodsObj: this.data.goodsObj
            }
            this.triggerEvent('eventNum', eventDetailNum)
          }

        })
      } else {
        //是否限购
        if (this.data.limitcount > 0) {
          count > 1 ? count-- : 1

        } else {
          count = --count
        }
        let eventDetailNum = {
          val: count,
          type: 'addevent'
        }
        this.triggerEvent('eventNum', eventDetailNum)
      }




      // this.triggerEvent('subevent')
    },

    //加
    add: function () {
      let count = this.data.count;
      //需要发请求修改数量
      if (this.data.goodsObj) {
        this.changeSum()
      }

      else {
        //是否限购
        if (this.data.limitcount > 0) {
          count >= this.data.limitcount ? this.data.limitcount : ++count;

        } else {
          count = ++count
        }
        let eventDetailNum = {
          val: count,
          type: 'addevent'
        }
        this.triggerEvent('eventNum', eventDetailNum)
      }
    },

    //修改购物车商品数量

    changeSum() {
      const self = this;
      let count = this.data.count;
      let ifReturn = false;
      if (this.data.limitcount > 0 && (this.data.limitcount <= this.data.count)) {
        wx.showToast({
          title: '该商品不能添加了噢',
          icon: 'none'
        })
        ifReturn = true;
      }

      if (ifReturn) return;
      console.log('ifReturn', ifReturn)
      // if (!ifReturn) {
      let goodsObj = this.data.goodsObj;
      console.log(' this.data.goodsObj', this.data.goodsObj);
      console.log('goodsObj', goodsObj)
      let cartUpdateGoodsNum = {
        cartItemId: goodsObj.cartItemId,
        goodsId: goodsObj.goodsId,
        num: count + 1,
        skuId: goodsObj.skuId,
        storeId: goodsObj.storeId
      }
      updateItemNum(cartUpdateGoodsNum).then(res => {
        if (res.code == 0) {
          count >= this.data.limitcount ? this.data.limitcount : ++count
          this.setData({
            count: count
          })
          let eventDetailNum = {
            val: count,
            type: 'addevent',
            goodsObj: self.data.goodsObj
          }
          this.triggerEvent('eventNum', eventDetailNum)
        }
      })
      //}


    },












    // this.triggerEvent('addevent')

  }
})
